var n = 0,
    a = 0;

Page({
    data: {
        scrollH: 0,
        imgWidth: 0,
        loadingCount: 0,
        images: [],
        imageData: [],
        col1: [],
        col2: []
    },
    onLoad: function () {
        wx.getSystemInfo({
            success: res => {
                var imgWidth = .48 * res.windowWidth,
                    scrollH = res.windowHeight;
                this.setData({
                    scrollH,
                    imgWidth
                })
                this.getImageData();
            }
        });
    },
    getImageData() {
        wx.cloud.callFunction({
            name: 'wanandroid',
            data: {
                action: 'beauty'
            }
        }).then(res => {
            console.log(res)
            let imageData = res.result
            this.setData({
                imageData
            })
            this.loadImages()
        })
    },
    loadImages: function () {
        let data = this.data.imageData
        let flag = "img-" + new Date()
        for (let i = 0; i < data.length; i++) {
            data[i].id = flag + "-" + i;
        }
        console.log(data.length)
        this.setData({
            loadingCount: data.length,
            images: data
        });
    },
    onImageLoad: function (e) {
        console.log(e);
        for (var t = e.currentTarget.id, i = e.detail.width, o = e.detail.height * (this.data.imgWidth / i), c = this.data.images, l = null, p = 0; p < c.length; p++) {
            var r = c[p];
            if (r.id === t) {
                l = r;
                break;
            }
        }
        l.height = o;
        var g = this.data.loadingCount - 1,
            h = this.data.col1,
            m = this.data.col2;
        n <= a ? (n += o, h.push(l)) : (a += o, m.push(l));
        var v = {
            loadingCount: g,
            col1: h,
            col2: m
        };
        g || (v.images = []), this.setData(v);
    },
    onImageClick(event) {
        console.log(event)
        let current = event.currentTarget.dataset.pic
        let urls = [current]
        wx.previewImage({
            current,
            urls
        })
    }
});